<?php

class PaymentOrderInfo extends PaymentData {

	public $_orderAr;
	public $_orderType;
// 	public $type;
	
	public function __construct(){
		$this->setName('orderInfo');
		$this->_orderAr = new Orders();
		$this->_orderType = ConstService::ORDER_TYPE_BUY_AUCTION;
	}
	
	public function setOrders($customerId,$auctionId){
 		$this->_orderAr->type = $this->_orderType;
		$ordersInfo = $this->_orderAr->find('t.customer_id = '.$customerId.' and auction_id='.$auctionId.' and t.quantity = '.$this->_orderType);
		$this->_orderAr = $ordersInfo;
		return $this;
	}
	
	public function setOrdersInfo($orderInfo){
		$this->_orderAr->attributes = $orderInfo;
		return $this;
	}
	
	public function setOrderInfoFromAuction($auctionInfo,$customerId){
		$time = time();
		$this->_orderAr->auction_id = $auctionInfo->id;
		$this->_orderAr->date_won = $time;
		if($auctionInfo->current_price>$auctionInfo->value_price){
			$auctionInfo->current_price = 0;
		}
		$this->_orderAr->price = $auctionInfo->current_price;
		$this->_orderAr->quantity = $auctionInfo->number;
		$this->_orderAr->total_price = ($auctionInfo->current_price+$auctionInfo->cost+$auctionInfo->handling_cost);
		$this->_orderAr->status = 1;
		$this->_orderAr->type = $this->_orderAr->type;
		$this->_orderAr->created = $time;
		$this->_orderAr->customer_id = $customerId;
		$this->_orderAr->shipping_fee = $auctionInfo->cost;
		$this->_orderAr->shipping_handling = $auctionInfo->handling_cost;
		$this->_orderAr->auction_sku = $auctionInfo->auction_sku;
		return $this;
	}
	
	public function saveInfo(){
		
		if($this->_orderAr->id){
			$orders = new Orders();
			$q = $orders->updateByPk($this->_orderAr->id,$this->_orderAr->attributes);
			$orders->afterUpdate($this->_orderAr);
			return $q;
		}else{
			$ordersInfo = new Orders();
			foreach ($this->toArray() as $key=>$val){
				$ordersInfo->$key = $val;
			}
			$ordersInfo->customer_id = $this->_orderAr->customer_id;
			

			$ordersInfo->save();
			$this->_orderAr->id = $ordersInfo->id;
			return $ordersInfo->id;
		}
	
	}
	
	public function orderVaild(){
		$orderBuyIntNow = Orders::model()->count(' customer_id=:customerId and type != '.ConstService::ORDER_TYPE_INCOMPLETE.' and auction_id = :auctionId',array(
				':customerId'	=> $this->_orderAr->customer_id,
				':auctionId'	=> $this->_orderAr->auction_id,
		));
		return $orderBuyIntNow > 1 ;
	}
		
	public function vaild(){
		if ($this->orderVaild()){
			$this->setErrorMessage('buyItNowExists','buy it now exists');
			return $this->vaild;
		}
		
		if (empty($this->_orderAr->customer_id)){
			$this->setErrorMessage('customerId','customerId NO');
			return $this->vaild;
		}
		
		$ordersInfo = $this->_orderAr->find('t.customer_id = '.$this->_orderAr->customer_id.' and auction_id='.$this->_orderAr->auction_id.' and t.type = '.$this->_orderType);
		if($ordersInfo->status != 1){
			$this->setErrorMessage('customerId','order status no 1');
			return $this->vaild;
		}
		if ($this->_orderAr->id <= 0 ){
			$this->setErrorMessage('orderNotId','order Does not exist');
			return $this->vaild;
		}
		
		return $this->vaild;
	}
	
	public function toArray(){
		$toArray = $this->_orderAr->attributes;
		return $toArray;
	}
	
	public function __get($value){
		return !empty($this->_orderAr->$value)?$this->_orderAr->$value:false;
	}
	
	public function __set($key,$value){
		return $this->_orderAr->$key = $value;
	}

}